package javaTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ASavetheMagazines {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {
        int l = Integer.parseInt(br.readLine());
        while (l-- > 0) {
            solve();
        }
        br.close();
    }

    static int n;
    static int numSum;
    static Integer[] a;
    static long max = 0l;

    public static void solve() throws IOException {
        n = Integer.parseInt(br.readLine());
        String str = br.readLine();
        long ans = 0;
        numSum = 0;
        int[] GaiZi = new int[n];
        for (int i = 0; i < n; i++) {
            GaiZi[i] = str.charAt(i) - '0';
            if (GaiZi[i] == 1)
                numSum++;
        }
        a = new Integer[n];
        String numberStr = br.readLine();
        String[] numbers = numberStr.split(" ");
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(numbers[i]);
        }
        max = 0l;
        dfs(0, 0, new int[numSum], GaiZi);
        System.out.println(max);
    }

    public static void dfs(int i, int index, int[] num, int[] GaiZi) {
        if (i == n) {
            long sum = 0;
            for (int l = 0; l < numSum; l++)sum += num[l];
            max = Math.max(sum, max);
            return;
        }
        if (GaiZi[i] == 1) {
            if (GaiZi[i - 1] == 0) {
                num[index] = a[i - 1];
                GaiZi[i] = 0;
                dfs(i + 1, index + 1, num, GaiZi);
                GaiZi[i] = 1;
            }
            num[index] = a[i];
            dfs(i + 1, index + 1, num, GaiZi);
        } else {
            dfs(i + 1, index, num, GaiZi);
        }
    }
}
